//工具

//清空本地存储
function clearAll() {
	localStorage.clear();
}
//根据关键字移除本地，（没用到）
function removeItem(key) {
	localStorage.removeItem(key);
}
//设置本地存储 
function setItem(k, v) {
	localStorage.setItem(k, v);
}
//获取本地存储
function getItem(key) {
	return localStorage.getItem(key);
}



//Json变字符串
function JsonToString(json) {

	return JSON.stringify(json)
}

//字符串变Json
function StringToJson(str) {
	return eval("(" + str + ")");
}
//是否为空
function isEmpty(str) {

	if(str == undefined || str == null || str == "" || str == 'null' || str.length == 0) {
		return true;
	} else {
		return false;
	}
}

//根据分隔符（，）拆分字符串,转为int数组 str:字符串，o:分隔符
function splitInt(str, o) {
	var tmparr = str.split(o);
	var tmpint = [];
	for(i = 0; i < tmparr.length; i++) {
		tmpint[i] = parseInt(tmparr[i]);
	}
	return tmpint;
}

//------------------cookie存储,主要使用在跳转界面之间参数的传递
//设置cookie
function setCookie(name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	if(expires != null) {
		var LargeExpDate = new Date();
		//存储的时间为1天
		LargeExpDate.setTime(LargeExpDate.getTime() + (expires * 1000 * 3600 * 24));
	}
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + LargeExpDate.toGMTString()));
}
//获取cookie
function getCookie(name) {
	var search = name + "="
	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if(offset != -1) {
			offset += search.length
			end = document.cookie.indexOf(";", offset)
			if(end == -1) end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		} else return ""
	}
}

//删除cookie
function deleteCookie(name) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
	setCookie(name, "", expdate);
}
//清空cookie
function clearCookie(){ 
	var keys=document.cookie.match(/[^ =;]+(?=\=)/g); 
	if (keys) { 
		for (var i = keys.length; i--;) 
		document.cookie=keys[i]+'=0;expires=' + new Date( 0).toUTCString() 
	} 
} 

//判断本地有没有登录
function isRegister() {
	var username = getCookie(getUserName());
	var passWord = getCookie(getPassWord());
	var userid = getCookie(getUserId());
	var boolean = false;
	//console.log(username+"==="+passWord);
	if(!isEmpty(username)) { //表示有存储账号
		if(!isEmpty(passWord)) { //有账号肯定有密码
			if(!isEmpty(userid)){
				boolean = true;
			}
			
		}
	}
	return boolean;
}//http://127.0.0.1:8020/ceramicmall/img/banner.png
//账号的id,登录成功会返回
function getUserId(){
	return "userid";
}
function getUserName() {
	return "username";
}

function getPassWord() {
	return "password";
}
/*电话正则*/
function checkMobile(B) {
	var A = /^1[3456789]\d{9}$/;
	if(A.test(B)) {
		return true
	} else {
		return false
	}
}

function post(url, data, callback, errorcallback,button) {
	if(button != null){
		if(button.hasClass("jingyong")){
			return;
		}
		button.addClass("jingyong");
	
	}
	$.post(url, data, function(res) {
		printLog(JsonToString(res));
		if(callback != null) {
			callback(res);
		}
		if(button != null){
		    button.removeClass("jingyong")
		}
	}, 'json').error(function() {
		if(errorcallback != null) {
			errorcallback();
		}
		if(button != null){
		    button.removeClass("jingyong")
		}
	});
}
function post2(url, data, callback, errorcallback,button) {
	
	if(button != null){
		if(button.hasClass("jingyong")){
			return;
		}
		button.addClass("jingyong");
	}
	
	$.post(url, data, function(res) {
		
		printLog(JsonToString(res));
		if(callback != null&&res.flag==0) {
			callback(res.data);
		}
		if(button != null){
		    button.removeClass("jingyong")
		}
	}, 'json').error(function() {
		if(errorcallback != null) {
			errorcallback();
		}
		if(button != null){
		    button.removeClass("jingyong")
		}
	});
}
function printLog(res){
	//正式的时候注释掉
	console.log(res);
}
function get(url, data, callback, errorcallback) {
	var datas = parseParams(data);
	if(!isEmpty(datas)){
		url+="?"+datas;
	}
	$.get(url, data, function(res) {
		printLog(JsonToString(res));
		if(callback != null) {
			callback(res);
		}
	}, 'json').error(function() {
		if(errorcallback != null) {
			errorcallback();
		}
	});
}
function get2(url, data, callback, errorcallback) {
	var datas = parseParams(data);
	if(!isEmpty(datas)){
		url+="?"+datas;
	}
	$.get(url, data, function(res) {
		printLog(JsonToString(res));
		if(callback != null&&res.flag==0) {
			callback(res.data);
		}
	}, 'json').error(function() {
		if(errorcallback != null) {
			errorcallback();
		}
	});
}
function parseParams(source) {
	var result = "";
	var i = 0;
	for(var key in source) {
		if(i > 0) {
			result += "&";
		}
		if(typeof source[key] === 'object' && source[key] != null) {

			result += key + "=" + JSON.stringify(source[key]);
		} else {
			if(key != "$$hashKey") {
				result += key + "=" + source[key];
			}
		}

		//result[key] =  ? copyObj(source[key]) : source[key];
		i++;
	}
	return result;
}
function parseFloats(a) {
	return parseFloat(a).toFixed(2);
}
function getMymoney(){
	get2(apihost+"getMymoney","",function(data){
		$(".myyue").text(data);
	});
}
